package com.dp.mallchat.common.user.dao;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dp.mallchat.common.common.domain.enums.DeleteStatusEnum;
import com.dp.mallchat.common.common.domain.vo.req.CursorPageBaseReq;
import com.dp.mallchat.common.common.domain.vo.resp.CursorPageBaseResp;
import com.dp.mallchat.common.common.utils.CursorUtils;
import com.dp.mallchat.common.user.domain.user.entity.User;
import com.dp.mallchat.common.user.domain.user.enums.UserActiveEnum;
import com.dp.mallchat.common.user.domain.ws.enums.ChatActiveStatusEnum;
import com.dp.mallchat.common.user.mapper.UserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author <a href="https://github.com/adu">adu</a>
 * @since 2024-05-14
 */
@Service
public class UserDao extends ServiceImpl<UserMapper, User> {

    @Resource
    private UserMapper userMapper;


    public User getUserByOpenId(String openId) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>()
                .eq(User::getOpenId, openId);
        return getOne(wrapper);
    }

    public void registerUser(User registerUser) {
        save(registerUser);
    }

    /**
     * 更新用户信息
     * @param user
     */
    public void updateUserInfo(User user) {
        updateById(user);
    }

    public User getByName(Long uid, String name) {
        return lambdaQuery()
                .eq(User::getName, name)
                .one();
    }

    public void updateById(Long uid, String name) {
        lambdaUpdate().eq(User::getId, uid)
                .set(User::getName, name)
                .update();
    }

    public void wearingBadgeById(Long uid, Long badgeId) {
        lambdaUpdate()
                .set(User::getItemId, badgeId)
                .eq(User::getId, uid)
                .update();
    }

    public List<User> getActiveUser(List<Long> uIds) {
        if (CollectionUtils.isEmpty(uIds)) {
            return Collections.EMPTY_LIST;
        }
        return lambdaQuery()
                .select(User::getId,User::getActiveStatus)
                .in(User::getId, uIds)
                .eq(User::getActiveStatus, UserActiveEnum.ACTIVE.getStatus())
                .eq(User::getStatus, DeleteStatusEnum.NORMAL.getStatus())
                .list();
    }

    public CursorPageBaseResp<User> getCursorPage(List<Long> memberUidList, CursorPageBaseReq request, ChatActiveStatusEnum online) {
        return CursorUtils.getCursorPageByMysql(this, request, wrapper -> {
            wrapper.eq(User::getActiveStatus, online.getStatus());//筛选上线或者离线的
            wrapper.in(com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(memberUidList), User::getId, memberUidList);//普通群对uid列表做限制
        }, User::getLastOptTime);
    }

    public Integer getOnlineCount(List<Long> memberUidList) {
        return lambdaQuery()
                .eq(User::getActiveStatus, ChatActiveStatusEnum.ONLINE.getStatus())
                .in(CollectionUtil.isNotEmpty(memberUidList), User::getId, memberUidList)
                .count();
    }

    public List<User> getMemberList() {
        return lambdaQuery()
                .eq(User::getStatus, DeleteStatusEnum.NORMAL.getStatus())
                .orderByDesc(User::getLastOptTime)//最近活跃的1000个人，可以用lastOptTime字段，但是该字段没索引，updateTime可平替
                .last("limit 1000")//毕竟是大群聊，人数需要做个限制
                .select(User::getId, User::getName, User::getAvatar)
                .list();
    }
}
